package rd.hardcore.engine.sound 
{
	import flash.events.EventDispatcher;
	import flash.events.SampleDataEvent;
	import flash.media.Sound;
	import flash.utils.ByteArray;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class HardcoreSoundModifier extends EventDispatcher
	{
		/*	Taille du paquet lu	*/
		public static const BYTES_PER_CALLBACK	: int = 4096; 
		/*	Pas de précision de la courbe	*/
		public static var SPECTRUM_PRECISION 	: int = 8;
		/*	vitesse de lecture	*/
		internal static var PLAYBACK_SPEED:Number = 1;	
		
		private var phase:Number;
		private var sourceMP3:Sound;
		
		public function HardcoreSoundModifier(source:Sound) 
		{
			sourceMP3 = source;
			phase = 0;
		}
		
		public function onSampleData(e:SampleDataEvent):void
		{
			var l:Number;
			var r:Number;
			var memL:Number;
			var memR:Number;
			var cpt:int = SPECTRUM_PRECISION;
			var p:int;
			
			var ba:ByteArray = new ByteArray();
			var startPosition:int = phase >> 0;
			sourceMP3.extract(ba, BYTES_PER_CALLBACK * PLAYBACK_SPEED);
			ba.position = 0;
			
			
			while (ba.bytesAvailable > 0)
			{
				p = int(phase - startPosition) * 8;
				
				if (p < ba.length - 8 && e.data.length <= BYTES_PER_CALLBACK * 8) {
					
					ba.position = p;
					l = ba.readFloat();
					r = ba.readFloat();
					
					/*if(HardcoreGhettoSoundSystem.PSEUDO_8BITS){
						if (cpt == SPECTRUM_PRECISION || SPECTRUM_PRECISION == 0) {
							memL = l;
							memR = r;
							cpt = 0;
						}
						
						e.data.writeFloat(memL); 
						e.data.writeFloat(memR);
						
						cpt++;
					}else {*/
						e.data.writeFloat(l); 
						e.data.writeFloat(r);
					//}
					
				}else {
					ba.position = ba.length;
				}
				phase += PLAYBACK_SPEED;
			
			}
		}
		
		
	}

}